package com.wang.utils.queueentity;

import java.util.Scanner;

/**
 * @author 10570
 * @date 2021/7/11 21:09
 */

/**
 * 数组模拟队列
 */
public class ArrayToQueueDemo {
    public static void main(String[] args) {
        //创建一个队列--一次性队列--一次之后就没有了，没有复用的效果
        ArrayQueue arrayQueue = new ArrayQueue(3);
        char key = '0';//
        System.out.println("测试对列模拟数组：");
        Scanner scanner = new Scanner(System.in);
        boolean loop = true;
        while (loop){
            System.out.println("show: 显示队列");
            System.out.println("exit: 退出");
            System.out.println("add: 添加");
            System.out.println("get: 获取");
            System.out.println("head:获取头数据");
            key = scanner.next().charAt(0);
            switch (key){
                case 's':
                    arrayQueue.showQueue();
                    break;
                case 'a':
                    int value = scanner.nextInt();
                    arrayQueue.addQueue(value);
                    break;
                case 'h':
                    try {
                        int i = arrayQueue.headQueue();
                        System.out.printf("头数据%d\n",i);
                    }catch (Exception e){
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'g':
                    try {
                        int queue = arrayQueue.getQueue();
                        System.out.printf("取出的数据%d\n",queue);
                    }catch (Exception e){
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'e':
                    scanner.close();
                    loop = false;
                    break;
                default:
                    break;
            }
        }
        System.out.println("退出");

    }
}

//编写数组模拟类 arrayQueue类
class ArrayQueue{
    private int maxSize; // 队列容量
    private int front ; //队列头
    private int rear; //队列尾
    private int arr[] ;

    //创建队列的构造器
    public ArrayQueue(int arrMaxSize){
        maxSize = arrMaxSize;
        arr = new int[maxSize];
        front = -1; //指向对猎头的前一个位置
        rear = -1; //指向队列尾，包含队列尾的数据
    }

    //判断队列是否满
    public boolean isFull(){
        return rear == maxSize-1;
    }
    //判断队列是否为空
    public boolean isEmpty(){
        return rear == front;
    }

    //添加数据到队列
    public void addQueue(int n){
        //判断队列满
        if (isFull()){
            System.out.println("队列满");
            return;
        }
        rear++;
        arr[rear] = n;

    }
    //数据出队列
    public int getQueue(){
        //判断队列空
        if (isEmpty()){
            throw new RuntimeException("队列空");
        }
        front ++;
        return arr[front];
    }
    //显示所有数据
    public void showQueue(){
        if (isEmpty()){
            System.out.println("队列空空");
            return;
        }
        for (int i = 0 ;i < arr.length;i++){
            System.out.printf("arr[%d] = %d \n",i,arr[i]);
        }
    }

    //显示头数据
    public int headQueue(){
        if (isEmpty()){
            throw new RuntimeException("队列空");
        }
        return arr[front+1];
    }

}
